#include "widget.h"
#include "file.h"
#include<QIcon>
#include<QPainter>
#include<heroplane.h>
#include<QMouseEvent>
#include<QPixmap>
#include <QKeyEvent>
#include <QMessageBox>
#include <QRandomGenerator>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    initScene();
    playgame();
}

Widget::~Widget() {}
void Widget::initScene()
{
    setFixedSize(GAME_WIDTH,GAME_HEIGHT);
    setWindowTitle(GAME_TITLE);
    setWindowIcon(QIcon(":/image/bg2"));
    m_Timer.setInterval(GAME_RATE);
    m_recorder = 0;
    m_star_recorder = 0;
    m_score = 0;
    m_star_count = 0;
}

void Widget::playgame()
{
    m_Timer.start();
    connect(&m_Timer,&QTimer::timeout,[=](){
        enemytoscene();
        startoscene();
        updateposition();
        update();
        collisiondetection();
    });
}

void Widget::updateposition()
{
    m_map.mapPosition();
    m_hero.shoot();
    for(int i=0;i<BULLET_NUM;i++){
        if(m_hero.m_bullets[i].m_free==false){
            m_hero.m_bullets[i].updateposition();
        }
    }
    for(int i=0;i<EMENY_NUM;i++){
        if(m_enemys[i].m_free==false){
            m_enemys[i].updateposition();
        }
    }

    for(int i=0;i<STAR_NUM;i++){
        if(m_stars[i].m_free==false){
            m_stars[i].updatePosition();
        }
    }
}

void Widget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.drawPixmap(0,m_map.m_map1_posX,m_map.m_map1);
    painter.drawPixmap(0,m_map.m_map2_posX,m_map.m_map2);
    painter.drawPixmap(m_hero.m_x,m_hero.m_y,m_hero.m_plane);
    for(int i=0;i<BULLET_NUM;i++){
        if(m_hero.m_bullets[i].m_free==false){
            painter.drawPixmap(m_hero.m_bullets[i].m_x,m_hero.m_bullets[i].m_y,m_hero.m_bullets[i].m_bullet);
        }
    }
    for(int i=0;i<EMENY_NUM;i++){
        if(m_enemys[i].m_free==false){
            painter.drawPixmap(m_enemys[i].m_x,m_enemys[i].m_y,m_enemys[i].m_enemy);
        }
    }

    for(int i=0;i<STAR_NUM;i++){
        if(m_stars[i].m_free==false){
            painter.drawPixmap(m_stars[i].m_x,m_stars[i].m_y,m_stars[i].m_star);
        }
    }


    painter.setPen(Qt::white);
    painter.setFont(QFont("Arial", 20, QFont::Bold));
    painter.drawText(GAME_WIDTH-150, 40, QString("分数: %1").arg(m_score));


    painter.setPen(Qt::white);
    painter.setFont(QFont("Arial", 20, QFont::Bold));
    painter.drawText(GAME_WIDTH-150, 80, QString("星星: %1").arg(m_star_count));
}


void Widget::keyPressEvent(QKeyEvent *event)
{
    switch(event->key()) {
    case Qt::Key_Left:
        m_hero.moveLeft();
        break;
    case Qt::Key_Right:
        m_hero.moveRight();
        break;
    case Qt::Key_Up:
        m_hero.moveUp();
        break;
    case Qt::Key_Down:
        m_hero.moveDown();
        break;
    default:
        QWidget::keyPressEvent(event);
    }
    update();
}

void Widget::enemytoscene()
{
    m_recorder++;
    if(m_recorder<EMENY_INTERVAL){
        return;
    }
    m_recorder=0;
    for(int i=0;i<EMENY_NUM;i++){
        if(m_enemys[i].m_free){
            m_enemys[i].m_free=false;
            m_enemys[i].m_x = rand()%(GAME_WIDTH-m_enemys[i].m_Rect.width());
            m_enemys[i].m_y = -m_enemys[i].m_Rect.height();
            break;
        }
    }
}

void Widget::startoscene()
{
    m_star_recorder++;
    if(m_star_recorder < STAR_INTERVAL){
        return;
    }
    m_star_recorder = 0;
    for(int i=0;i<STAR_NUM;i++){
        if(m_stars[i].m_free){
            m_stars[i].m_free = false;
            m_stars[i].m_x = rand()%(GAME_WIDTH-m_stars[i].m_rect.width());
            m_stars[i].m_y = -m_stars[i].m_rect.height();
            break;
        }
    }
}

void Widget::collisiondetection()
{

    for(int i=0;i<EMENY_NUM;i++){
        if(m_enemys[i].m_free){
            continue;
        }
        for(int j=0;j<BULLET_NUM;j++){
            if(m_hero.m_bullets[j].m_free){
                continue;
            }
            if(m_enemys[i].m_Rect.intersects(m_hero.m_bullets[j].m_rect)){
                m_enemys[i].m_free=true;
                m_hero.m_bullets[j].m_free=true;
                switch(m_enemys[i].m_type) {
                case 1:
                    m_score += 1;
                    break;
                case 2:
                    m_score += 1;
                    break;
                case 3:
                    m_score += 1;
                    break;
                case 4:
                    m_score += 1;
                    break;
                case 5:
                    m_score += 1;
                    break;
                }
            }
        }


        if(m_hero.m_Rect.intersects(m_enemys[i].m_Rect)){
            m_enemys[i].m_free = true;
            QMessageBox::warning(this, "注意安全", "冒险也要注意安全哦！");
        }
    }


    for(int i=0;i<STAR_NUM;i++){
        if(m_stars[i].m_free){
            continue;
        }
        if(m_hero.m_Rect.intersects(m_stars[i].m_rect)){
            m_stars[i].m_free = true;
            m_star_count++;



        }
    }


    if (m_score >= 50&&m_star_count>=20) {

        m_Timer.stop();
        QMessageBox::information(this, "游戏成功", "恭喜你，离开了幻格岛");
        close();
    }
}


